home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Files / XTND 1.3.6 / Translator Examples / Text Translator / TextExport.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-23  |  10.2 KB  |  251 lines  |  [TEXT/MPS ]

  1. /************************************************************************
  2. *                                                                        *
  3. *    TextExport.c                                                        *
  4. *                                                                        *
  5. *    Translator for writing a text file with XTND 1.3.  This translator    *
  6. *    does not support headers, footers, pictures, or footnotes, since    *
  7. *    it writes only simple, unstructured ASCII files.                    *
  8. *                                                                        *
  9. *    Copyright © 1989,90 Claris Corporation                                *
  10. *    All Rights Reserved                                                    *
  11. *                                                                        *
  12. *    Author: Lyndley Crumly                                                *
  13. *    Date:   12 February, 1990                                            *
  14. *                                                                        *
  15. ************************************************************************/
  16.  
  17.  
  18. /***** Note: This project MUST be compiled and merged after TextImport.π   *****/
  19. /*****       is compiled, since that project replaces the resources in the *****/
  20. /*****       Text translator when it is built, including the resource      *****/
  21. /*****       created by this project.                                      *****/
  22.  
  23. #include <StandardFile.h>
  24. #include <Memory.h>
  25. #include <Printing.h>
  26.  
  27. #include ":::XTND Headers:XTNDCIncludes:XTNDTextTranslator.h"
  28.  
  29.  
  30. /*------------------------- Useful Constants ---------------------------*/
  31.  
  32. #define kBufferSize    512
  33. #define kSpaceChar     0x20
  34.  
  35.  
  36. /*-------------------------Global Variables-----------------------------*/
  37.  
  38. /* The translator globals structure is used to store our import         */
  39. /* translator’s translator’s private global variables. A relocatable     */
  40. /* block is created in the application heap when the translator         */
  41. /* receives the EXPORT_INIT directive and a handle to the globals         */
  42. /* are stored in the GlobalHandle field of the Export Parameter Block     */
  43. /* passed into the translator from the application. The globals block     */
  44. /* is freed when the translator receives the EXPORT_CLOSE_ALL directive.*/
  45.  
  46. /* We are going to lock down the global handle when it is created and    */
  47. /* leave it that way so as to avoid any problems with dereferenced        */
  48. /* pointers.  You may choose to unlock the global handle when feasible.    */
  49.  
  50. typedef struct TransGlobals {
  51.     unsigned char     tempBuffer[kBufferSize];    /* Memory buffer where data to be written to the disk file is stored    */
  52.     long            filePos;                /* This stores our current file position.  Since the file is written linearly from start to finish, this is always the next position in the file that we will write to.    */
  53.     long            tempBufferPos;            /* This stores the current amount of text in the memory buffer which has not yet been written to the output file    */
  54. } TMyGlobals, *TMyGlobalPtr, **TMyGlobalHandle;
  55.  
  56.  
  57.  
  58. /*-------------------------Function prototypes--------------------------*/
  59.  
  60.        void    main(ExportParmBlkPtr exportPBPtr);
  61. static void CloseMain(ExportParmBlkPtr exportPBPtr, TMyGlobalPtr myGlobalPtr);
  62. static void WriteText(ExportParmBlkPtr exportPBPtr, TMyGlobalPtr myGlobalPtr);
  63. static void PutChar(char ch, ExportParmBlkPtr exportPBPtr, TMyGlobalPtr myGlobalPtr);
  64. static void WriteTempBuffer(long position, long count, unsigned char *bufptr, 
  65.                         ExportParmBlkPtr exportPBPtr, TMyGlobalPtr myGlobalPtr);
  66.  
  67.  
  68. /*----------------------------------------------------------------------*/
  69. /*    main    This is the main routine and the only entry point for the    */
  70. /*            the translator.  This routine actually performs all of the    */
  71. /*            work of the translator.  It dispatches the call to the         */
  72. /*            appropriate routine depending on the directive and             */
  73. /*            translator state.                                            */
  74. /*----------------------------------------------------------------------*/
  75. void main(ExportParmBlkPtr exportPBPtr)
  76. {
  77.     switch(exportPBPtr->directive)
  78.     {
  79.         case exportInitAll:            /* Initialize the translator.  This must be    */
  80.                                     /* the first call to the translator.          */
  81.             if (!(exportPBPtr->globalHandle = NewHandle(sizeof(TMyGlobals))))
  82.                 *exportPBPtr->result = MemError();
  83.             else {
  84.                 MoveHHi(exportPBPtr->globalHandle);
  85.                 HLock(exportPBPtr->globalHandle);
  86.                 ((TMyGlobalPtr)*exportPBPtr->globalHandle)->filePos = 0;
  87.                 ((TMyGlobalPtr)*exportPBPtr->globalHandle)->tempBufferPos = 0;
  88.             }
  89.             break;
  90.  
  91.         case exportOpenMain:        /* Initialize the main story.            */
  92.                                     /* Nothing needs to be done here.        */
  93.             exportPBPtr->directive = exportAcknowledge;
  94.             break;
  95.  
  96.         case exportWriteText:            /* Write the next text run to the file.    */
  97.                                     /* This translator only exports the     */
  98.                                     /* main body.                            */
  99.             WriteText(exportPBPtr, (TMyGlobalPtr)*exportPBPtr->globalHandle);
  100.             break;
  101.  
  102.         case exportCloseMain:        /* Close the main story when finished    */
  103.                                     /* with it.                                */
  104.             CloseMain(exportPBPtr, (TMyGlobalPtr)*exportPBPtr->globalHandle);
  105.             break;
  106.  
  107.         case exportCloseAll:        /* Shutting down.                        */
  108.             CloseMain(exportPBPtr, (TMyGlobalPtr)*exportPBPtr->globalHandle);
  109.             if (exportPBPtr->globalHandle) {
  110.                 DisposHandle(exportPBPtr->globalHandle);
  111.                 (exportPBPtr->globalHandle) = 0L;
  112.             }
  113.             break;
  114.  
  115.         case exportWriteResources:        /* We don't write any resources    */
  116.             exportPBPtr->directive = exportAcknowledge;
  117.             break;
  118.  
  119.         default:                    /* We don't show the directives that we */
  120.             break;                    /* don't use. They are all taken care   */
  121.                                     /* of by default, which does nothing    */
  122.                                     /* at all.                                */
  123.     }
  124. }
  125.  
  126.  
  127. /*----------------------------------------------------------------------*/
  128. /*    CloseMain    This routine closes the main story if it has not yet     */
  129. /*            been closed.  This mainly consists of writing any remaining    */
  130. /*            text from the memory buffer to the output file.                */
  131. /*----------------------------------------------------------------------*/
  132. static void CloseMain(register ExportParmBlkPtr exportPBPtr, 
  133.                       register TMyGlobalPtr myGlobalPtr)
  134. {
  135.     /* If there is text remaining in the buffer write it to the file.    */
  136.  
  137.     if (myGlobalPtr->tempBufferPos != 0) {    /* Check for text in the buffer */
  138.         WriteTempBuffer(myGlobalPtr->filePos, myGlobalPtr->tempBufferPos, 
  139.                             myGlobalPtr->tempBuffer, exportPBPtr, myGlobalPtr);
  140.         myGlobalPtr->tempBufferPos = 0;    /* Indicate that the buffer is empty    */
  141.     }
  142. }
  143.  
  144.  
  145. /*----------------------------------------------------------------------*/
  146. /*    WriteText    This routine writes each text run received from the        */
  147. /*            application.                                                */
  148. /*----------------------------------------------------------------------*/
  149. static void WriteText(register ExportParmBlkPtr  exportPBPtr, 
  150.                       register TMyGlobalPtr myGlobalPtr)
  151. {
  152.     char    ch;                /* The next character from the text buffer    */
  153.     char    *writeTextPtr;    /* Pointer to position in the text buffer    */
  154.     long     i;                                            /* Loop index    */ 
  155.     long    charsInRun = *exportPBPtr->textLength; /* Length of the run    */
  156.  
  157.     /* Since the output text buffer is a handle, unlike the input text    */
  158.     /* text buffer, we first lock the handle, and then set up a pointer    */
  159.     /* to our current position in the buffer.                            */
  160.     
  161.     HLock(exportPBPtr->textBuffer);                    /* Lock the buffer    */
  162.     writeTextPtr = *exportPBPtr->textBuffer; /* Set up pointer to text    */
  163.  
  164.     /* Process each character in the text buffer and write it to the    */
  165.     /* output file.  Some of the character conversion is arbitrary and    */
  166.     /* could equally be done some other way.                            */ 
  167.  
  168.     for (i = 0;i < charsInRun; i++)
  169.     {
  170.         ch = *writeTextPtr++;                /* Get the next character    */
  171.         switch (ch)
  172.         {
  173.             case softHyphen:        /* Discretionary hyphen character    */
  174.                 break;                /* This character is not written    */
  175.                 
  176.             case mergeBreak:                            /* Merge break    */
  177.             case newColumn:                                /* Column break    */
  178.             case newPage:                                /* Page break    */
  179.                 PutChar(returnChar, exportPBPtr, myGlobalPtr);
  180.                 break;    /* These are all changed to return characters    */
  181.  
  182.             case pageNumber:                              /* Page number    */
  183.             case footnoteChar:    /* Footnote reference within a footnote    */
  184.             case footnoteMark:    /* Footnote reference within main body    */
  185.             case floatingPict:                        /* Floating picture    */
  186.             case shortDateChar:                            /* Short date    */
  187.             case abbrDateChar:                        /* Abbreviated date    */
  188.             case longDateChar:                            /* Long date    */
  189.             case dayAbbrDateChar:        /* Abbreviated date with day    */
  190.             case dayLongDateChar:                  /* Long date with day    */
  191.             case timeChar:                                        /* Time */
  192.             case kSpaceChar:                            /* Space character    */
  193.                 PutChar(kSpaceChar, exportPBPtr, myGlobalPtr);
  194.                 break;    /* These are all changed to space characters    */
  195.  
  196.             default:                            /* All other characters    */
  197.                 PutChar(ch, exportPBPtr, myGlobalPtr);
  198.                 break;        /* These are written without any conversion    */
  199.         }
  200.  
  201.     }
  202.     HUnlock(exportPBPtr->textBuffer);            /* Unlock the buffer    */
  203. }
  204.     
  205.  
  206. /*----------------------------------------------------------------------*/
  207. /*    PutChar    This routine writes the given character to the memory         */
  208. /*            buffer and automatically writes the memory buffer to the    */
  209. /*            output file when it is full.                                */
  210. /*----------------------------------------------------------------------*/
  211. static void PutChar(char ch, 
  212.                     register ExportParmBlkPtr exportPBPtr, 
  213.                     register TMyGlobalPtr myGlobalPtr)
  214. {
  215.     if (*exportPBPtr->result) return;        /* Cancel if a previous error exists    */
  216.     
  217.     myGlobalPtr->tempBuffer[myGlobalPtr->tempBufferPos] = ch;    /* Put the char into the buffer    */
  218.     myGlobalPtr->tempBufferPos++;                    /* Increment buffer position    */
  219.     
  220.     if (myGlobalPtr->tempBufferPos == kBufferSize) {        /* If the buffer is full    */
  221.         WriteTempBuffer(myGlobalPtr->filePos, kBufferSize, myGlobalPtr->tempBuffer, 
  222.                     exportPBPtr, myGlobalPtr);
  223.         myGlobalPtr->tempBufferPos = 0;        /* Write the buffer to the output file    */
  224.     }
  225. }
  226.  
  227.  
  228. /*----------------------------------------------------------------------*/
  229. /*    WriteTempBuffer    This routine writes the specified number of bytes    */
  230. /*            from the temporary buffer to the output file starting        */
  231. /*            at the designated position in the file.                        */
  232. /*----------------------------------------------------------------------*/
  233. static void WriteTempBuffer(long position, 
  234.                             long count, 
  235.                             unsigned char *bufptr,
  236.                             register ExportParmBlkPtr exportPBPtr, 
  237.                             register TMyGlobalPtr myGlobalPtr)
  238. {
  239.     long    bytes = count;            /* Number of bytes to be written    */
  240.     
  241.     /* Set the file position for the data to be written */
  242.     if (*exportPBPtr->result = SetFPos(*exportPBPtr->refNum, fsFromStart, position))
  243.         return;
  244.  
  245.     /* Write the given number of bytes from the buffer to the file */
  246.     if (*exportPBPtr->result = FSWrite(*exportPBPtr->refNum, &bytes, bufptr))
  247.         return;
  248.  
  249.     myGlobalPtr->filePos += count;    /* Increment filePos for the next call    */
  250. }
  251.